/**
 * 
 */
package com.appspot.myjsf2;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;
import java.util.Map;
import java.util.Set;

import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * @author worker
 *
 */
public class TwitterCallbackServlet extends HttpServlet{
	public static String PARAMETER_NAME_VERIFIER="oauth_verifier";
	public static String PARAMETER_NAME_TOKEN="oauth_token";

	public void doGet(HttpServletRequest req, HttpServletResponse resp)
	throws IOException {
		Map parameters = req.getParameterMap();
		Set keys = parameters.keySet();
		StringBuilder sb = new StringBuilder();
		for(Object key: keys){
			System.out.println("Key: " + key);
			System.out.println("value: " + req.getParameter((String)key));
			sb.append("Key: " + key);
			sb.append("value: " + req.getParameter((String)key));
		}
		
		resp.setContentType("text/plain");
		PrintWriter writer = resp.getWriter();
		writer.println(sb.toString());
		try {
			System.out.println("trying to get data ...");
			String resultRaw = TwitterAuther.getData(req.getParameter(PARAMETER_NAME_VERIFIER));
			//List<String> contacts = GContactsParser.getContacts(resultRaw);
			System.out.println(resultRaw);
			writer.println(resultRaw);
			writer.println("Following contacts would be added into direct application");
			//for(String contact: contacts){
			//	writer.println(contact);
			//}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			System.out.println(e.getMessage());
			resp.getWriter().println("Error happened!");
			e.printStackTrace();
		} 
		//resp.getWriter().println("parameters in request: ");
		//resp.getWriter().println(sb.toString());
	}



}
